package com.xusiyan08.controller;

import com.xusiyan08.dto.UserUpdateRequest;
import com.xusiyan08.entity.Item;
import com.xusiyan08.entity.User;
import com.xusiyan08.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.server.ServerWebExchange;

import java.util.List;

@RestController
@RequestMapping("/api/admin")
@CrossOrigin(origins = "*")
public class AdminController {

    @Autowired
    private AdminService adminService;

    @GetMapping("/users")
    public ResponseEntity<List<User>> getAllUsers() {
        List<User> users = adminService.getAllUsers();
        return ResponseEntity.ok(users);
    }

    @DeleteMapping("/users/{userId}")
    public ResponseEntity<?> deleteUser(@PathVariable Long userId, ServerWebExchange exchange) {
        Long adminId = exchange.getRequiredAttribute("userId");
        adminService.deleteUser(userId, adminId, "Deleted by admin via controller");
        return ResponseEntity.ok("用户删除成功");
    }

    @GetMapping("/users/{userId}")
    public ResponseEntity<User> getUserById(@PathVariable Long userId) {
        User user = adminService.getUserById(userId);
        if (user != null) {
            return ResponseEntity.ok(user);
        } else {
            return ResponseEntity.notFound().build();
        }
    }

    @PutMapping("/users/{userId}")
    public ResponseEntity<?> updateUser(@PathVariable Long userId, @RequestBody UserUpdateRequest userUpdateRequest, ServerWebExchange exchange) {
        Long adminId = exchange.getRequiredAttribute("userId");
        String actionNotes = "User updated by admin via controller";

        User updatedUser = adminService.updateUser(userId, userUpdateRequest, adminId, actionNotes);
        if (updatedUser != null) {
            return ResponseEntity.ok(updatedUser);
        } else {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("更新用户失败");
        }
    }

    @GetMapping("/items/pending")
    public ResponseEntity<List<Item>> getPendingItems() {
        List<Item> items = adminService.getPendingItems();
        return ResponseEntity.ok(items);
    }

    @PutMapping("/items/{itemId}/approve")
    public ResponseEntity<?> approveItem(@PathVariable Long itemId, ServerWebExchange exchange) {
        Long adminId = exchange.getRequiredAttribute("userId");
        Item item = adminService.approveItem(itemId, adminId, "Approved by admin via controller");
        return ResponseEntity.ok(item);
    }

    @PutMapping("/items/{itemId}/reject")
    public ResponseEntity<?> rejectItem(@PathVariable Long itemId, ServerWebExchange exchange) {
        Long adminId = exchange.getRequiredAttribute("userId");
        Item item = adminService.rejectItem(itemId, adminId, "Rejected by admin via controller");
        return ResponseEntity.ok(item);
    }

    @DeleteMapping("/items/{itemId}")
    public ResponseEntity<?> deleteItem(@PathVariable Long itemId, ServerWebExchange exchange) {
        Long adminId = exchange.getRequiredAttribute("userId");
        adminService.deleteItem(itemId, adminId, "Deleted by admin via controller");
        return ResponseEntity.ok("商品删除成功");
    }

    @GetMapping("/items/all-details")
    public ResponseEntity<List<Item>> getAllItemsWithDetails() {
        List<Item> items = adminService.getAllItemsForAdmin();
        return ResponseEntity.ok(items);
    }
}